import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import org.msgpack.MessagePack;
import org.newbee.message.Animal;
import org.newbee.message.MessageHeader;
import org.newbee.message.User;
import org.newbee.msgpack.coder.MsgpackDecoder;
import org.newbee.msgpack.coder.MsgpackEncoder;
import org.newbee.proto.OrderProtocol;


/**
 * Created by iss on 17/6/2.
 */
public class NettyTestClient {

    public static ChannelFuture future;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String host = "127.0.0.1";
        int port = 8888;
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup);
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            bootstrap.option(ChannelOption.TCP_NODELAY, true);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
//                    ch.pipeline().addLast("msgpack decoder", new MsgpackDecoder());
//                    ch.pipeline().addLast("msgpack encoder", new MsgpackEncoder());
//                    ch.pipeline().addLast(new NettyTestClientHandler());
                    //         ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                    ch.pipeline().addLast(new ProtobufDecoder(OrderProtocol.Order.getDefaultInstance()));
                    ch.pipeline().addLast(new ProtobufEncoder());
                    ch.pipeline().addLast(new NettyTestClientHandler());
                }
            });
            // 发起异步连接操作
            future = bootstrap.connect(host, port).sync();
            Thread t = new Thread(new Runnable() {
                public void run() {
//                    boolean flag = true;
//                    BufferedReader br;
//                    while (flag) {
//                        try {
//                            br = new BufferedReader(new InputStreamReader(System.in));
//                            String in = br.readLine();
//                            if (in.equals("bye")) {
//                                flag = false;
//                            } else {
//                                ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(in.getBytes().length);
//                                byteBuf.writeBytes(in.getBytes("utf-8"));
//                                future.channel().writeAndFlush(byteBuf);
//                                //future.channel().writeAndFlush(new User());
//                            }
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }

//                    try {
//                        MessagePack pack=new MessagePack();
//                        MessageHeader messageHeader =new MessageHeader();
//                        User user = new User();
//                        user.setName("阿布");
//                        user.setAge(18);
//                        messageHeader.setContent(pack.write(user));
//                        messageHeader.setType(10);
//                        future.channel().writeAndFlush(messageHeader);
//                        Thread.sleep(2000);
//                        MessageHeader messageHeader2 =new MessageHeader();
//                        Animal animal =new Animal();
//                        animal.setName("狗");
//                        animal.setAge(25);
//                        messageHeader2.setContent(pack.write(animal));
//                        messageHeader2.setType(20);
//                        future.channel().writeAndFlush(messageHeader2);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
                    OrderProtocol.Order.Builder builder = OrderProtocol.Order.newBuilder();
                    builder.setTime(123);
                    builder.setDesc("阿斯达大神");
                    builder.setPrice(123);
                    builder.setUserid(123);
                    builder.setCorpus(OrderProtocol.Corpus.NEWS);
                    OrderProtocol.Payment.Builder builder1 = OrderProtocol.Payment.newBuilder();
                    builder1.setDesc("阿斯达大神");
                    builder1.setUserid(123);
                    builder1.setPrice(123);
                    builder1.setTime(123);
                    builder.addPayments(builder1.build());

                    future.channel().writeAndFlush(builder.build());
                }
            });
            t.start();
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            workerGroup.shutdownGracefully();
        }
    }

}
